#include "StaticObject.h"
#include "BaseObject.h"
#include "CreatePlayerDiedMessage.h"
#include "../SGD Wrappers/SGD_MessageManager.h"
#include "../SGD Wrappers/SGD_EventManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "Player.h"
#include "GameplayState.h"
#include "Game.h"
#include "Camera.h"
#include "Explosion.h"
#include <ctime>

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

StaticObject::StaticObject()
{
	m_eType = ObjectType::BACKGROUND;
	m_bWasTriggered = true;
}

StaticObject::~StaticObject()
{
	for (unsigned int j = 0; j < m_vtParticle.size(); j++)
	{
		m_vtParticle[j]->GetEmitter()->GetAliveVec().clear();

		while (m_vtParticle[j]->GetEmitter()->GetFlyweight()->GetRefCount() > 1)
			m_vtParticle[j]->GetEmitter()->GetFlyweight()->Release();

		delete m_vtParticle[j];
	}
	m_vtParticle.clear();

	if (m_aAudio != SGD::INVALID_HANDLE)
		SGD::AudioManager::GetInstance()->UnloadAudio(m_aAudio);
}

void StaticObject::Render()
{

	SGD::GraphicsManager* pGraphics = SGD::GraphicsManager::GetInstance();
	//BaseObject::Render();

	int x = GameplayState::GetInstance()->GetCamX();
	int y = GameplayState::GetInstance()->GetCamY();

	SGD::Point camera = { m_ptPosition.x - x, m_ptPosition.y - y };

	if (camera.x + m_szSize.width < 0
		|| camera.x - m_szSize.width  > Game::GetInstance()->GetScreenWidth()
		|| camera.y + m_szSize.height  < 0
		|| camera.y - m_szSize.height  > Game::GetInstance()->GetScreenHeight())
		return;


	//if (m_eType == ObjectType::GROUND)
	//	pGraphics->DrawRectangle(GetRect(), SGD::Color(12, 124, 5));
	//else if (m_eType == ObjectType::DEADLY)
	//	pGraphics->DrawRectangle(GetRect(), SGD::Color(255, 50, 50));
	//else
	//	pGraphics->DrawRectangle(GetRect(), SGD::Color(50, 50, 50));

	if (m_bAlive)
		pGraphics->DrawTextureSection(m_hImage, camera, m_rTextureSection);

	if (m_vtParticle.size() > 0 && m_bWasTriggered)
		m_vtParticle[0]->Render();
}
SGD::Rectangle StaticObject::GetRect() const
{
	//if (m_fRotation==0)
	return BaseObject::GetRect();

}
void StaticObject::Update(float dt)
{

	//if (GameplayState::GetInstance()->CameraUpdate())
	//{
	//	float x = (float)GameplayState::GetInstance()->GetCamX();
	//	float y = (float)GameplayState::GetInstance()->GetCamY();
	//
	//	SGD::Point position = { m_ptPosition.x - x*5*dt, m_ptPosition.y};
	//
	//	m_ptPosition = position;
	//}

	if (m_bWasTriggered)
		BaseObject::Update(dt);
	//m_ptPosition.x = GameplayState::GetInstance()->GetCamX();
}

void StaticObject::HandleCollision(BaseObject* pOther)
{
	//if (dynamic_cast<Explosion*>(pOther) && m_eType == ObjectType::Safe)
	//{
	//	delete pOther;
	//	pOther = nullptr;
	//
	//	return;
	//}
	if (dynamic_cast<MovingObject*>(pOther))
	{
		return;
	}

	if (!dynamic_cast<Explosion*>(pOther))
	{

		switch (m_eType)
		{
		case ObjectType::SPEEDPOTION:
		{
										m_bAlive = false;
										dynamic_cast<Player*>(pOther)->SetPotionMod(2);

										if (m_aAudio == SGD::INVALID_HANDLE)
											m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Potion.wav");

										if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
											SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
		}
			break;
		case ObjectType::DEADLY:
		{
								   //SGD::MessageManager::GetInstance()->QueueMessage(new CreatePlayerDiedMessage());
								   pOther->SetAlive(false);
		}
			break;

		case ObjectType::SLOWPOTION:
		{
									   if (m_aAudio == SGD::INVALID_HANDLE)
										   m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Potion.wav");

									   if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
										   SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
									   m_bAlive = false;
									   dynamic_cast<Player*>(pOther)->SetPotionMod(0.5f);
									   break;
		}

		case ObjectType::FASTPOTION:
		{
									   if (m_aAudio == SGD::INVALID_HANDLE)
										   m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Potion.wav");

									   if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
										   SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
									   m_bAlive = false;
									   dynamic_cast<Player*>(pOther)->SetPotionMod(1.5f);

									   break;
		}
		case ObjectType::Safe:
		{
								 GameplayState::GetInstance()->SetIsActiveTimer(false);
								 SGD::EventManager::GetInstance()->QueueEvent(new SGD::Event("GateDrop"));
								 break;
		}
		case ObjectType::GateUp:
		{
								 GameplayState::GetInstance()->SetIsActiveTimer(true);
								 SGD::EventManager::GetInstance()->QueueEvent(new SGD::Event("GateRaise"));
								 break;
		}
		case ObjectType::NotSafe:
		{
									GameplayState::GetInstance()->SetIsActiveTimer(true);
									break;
		}
		case ObjectType::PortalMenu:
		{
									   if (m_aAudio == SGD::INVALID_HANDLE)
										   m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Portal.wav");

									   if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
										   SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
									   
									   float time = 0;
									   unsigned long m_ulGameTime = 0;

									   while (true)
									   {
										   // Current time
										   unsigned long now = GetTickCount();
										   float dt = (now - m_ulGameTime) / 1000.f;
										   m_ulGameTime = now;

										   // Cap the elapsed time to 1/8th of a second
										   if (dt > 0.125f)
											   dt = 0.125f;

										   time += dt;
										   if (time > 1.5f)
											   break;
									   }

									   unsigned char portal = 0;
									   portal = portal | (1 << 0);
									   GameplayState::GetInstance()->SetPortal(portal);
									   break;
		}
		case ObjectType::PortalNext:
		{
									   if (m_aAudio == SGD::INVALID_HANDLE)
										   m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Portal.wav");

									   if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
										   SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
									  
									   float time = 0;
									   unsigned long m_ulGameTime = 0;

									   while (true)
									   {
										   // Current time
										   unsigned long now = GetTickCount();
										   float dt = (now - m_ulGameTime) / 1000.f;
										   m_ulGameTime = now;

										   // Cap the elapsed time to 1/8th of a second
										   if (dt > 0.125f)
											   dt = 0.125f;

										   time += dt;
										   if (time > 1.5f)
											   break;
									   }

									   unsigned char portal = 0;
									   portal = portal | (1 << 1);
									   GameplayState::GetInstance()->SetPortal(portal);
									   break;
		}
		case ObjectType::PortalReset:
		{
										if (m_aAudio == SGD::INVALID_HANDLE)
											m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Portal.wav");

										if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
											SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
										
										float time = 0;
										unsigned long m_ulGameTime =0;

										while (true)
										{
											// Current time
											unsigned long now = GetTickCount();
											float dt = (now - m_ulGameTime) /1000.f;
											m_ulGameTime = now;

											// Cap the elapsed time to 1/8th of a second
											if (dt > 0.125f)
												dt = 0.125f;

											time += dt;
											if (time > 1.5f)
												break;
										}

										SGD::MessageManager::GetInstance()->QueueMessage(new CreatePlayerDiedMessage());
										break;
		}

		case ObjectType::LandMine:
		{
			//m_bAlive = false;
			if (!dynamic_cast<Player*>(pOther)->GetDeathPot())
				pOther->SetAlive(false);
			if (m_aAudio == SGD::INVALID_HANDLE)
			 m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Land_Mine.wav");

			if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
			 SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
			break;
		}
		case ObjectType::TeslaCoil:
		{
									  if (m_aAudio == SGD::INVALID_HANDLE)
										  m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Shock.wav");

									  if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
										  SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
									  break;
		}
		case ObjectType::Wire:
		{
									  if (m_aAudio == SGD::INVALID_HANDLE)
										  m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Shock.wav");

									  if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
										  SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
									  break;
		}
		case ObjectType::NODEATHPOTION:
		{
										  if (m_aAudio == SGD::INVALID_HANDLE)
											  m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Potion.wav");

										  if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
											  SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
										  break;
		}
		case ObjectType::NOFRICTIONPOTION:
		{
										  if (m_aAudio == SGD::INVALID_HANDLE)
											  m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Potion.wav");

										  if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
											  SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
										  break;
		}
		case ObjectType::AcidPool:
		{
											 if (m_aAudio == SGD::INVALID_HANDLE)
												 m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Acid.wav");

											 if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio))
												 SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
											 break;
		}
		}
	}
}